package com.moxiao.yfjh.module.blind.service.recommendRecord;

import com.moxiao.yfjh.module.blind.controller.admin.recommendRecord.vo.RecommendRecordCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.recommendRecord.vo.RecommendRecordExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.recommendRecord.vo.RecommendRecordPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.recommendRecord.vo.RecommendRecordUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.recommendRecord.RecommendRecordConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.recommendRecord.RecommendRecordDO;
import com.moxiao.yfjh.module.blind.dal.mysql.recommendRecord.RecommendRecordMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.recommendRecord.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲超级推荐记录 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class RecommendRecordServiceImpl implements RecommendRecordService {

    @Resource
    private RecommendRecordMapper recommendRecordMapper;

    @Override
    public Long createRecommendRecord(RecommendRecordCreateReqVO createReqVO) {
        // 插入
        RecommendRecordDO recommendRecord = RecommendRecordConvert.INSTANCE.convert(createReqVO);
        recommendRecordMapper.insert(recommendRecord);
        // 返回
        return recommendRecord.getId();
    }

    @Override
    public void updateRecommendRecord(RecommendRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateRecommendRecordExists(updateReqVO.getId());
        // 更新
        RecommendRecordDO updateObj = RecommendRecordConvert.INSTANCE.convert(updateReqVO);
        recommendRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecommendRecord(Long id) {
        // 校验存在
        validateRecommendRecordExists(id);
        // 删除
        recommendRecordMapper.deleteById(id);
    }

    private void validateRecommendRecordExists(Long id) {
        if (recommendRecordMapper.selectById(id) == null) {
            throw exception(RECOMMEND_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RecommendRecordDO getRecommendRecord(Long id) {
        return recommendRecordMapper.selectById(id);
    }

    @Override
    public List<RecommendRecordDO> getRecommendRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return recommendRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RecommendRecordDO> getRecommendRecordPage(RecommendRecordPageReqVO pageReqVO) {
        return recommendRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RecommendRecordDO> getRecommendRecordList(RecommendRecordExportReqVO exportReqVO) {
        return recommendRecordMapper.selectList(exportReqVO);
    }

}
